﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Input;
using PlaneWar.Controls;
using PlaneWar.Foundation;
using System.Collections;

namespace PlaneWar.Helper
{

    public enum direction_X
    {
        Stop,
        Left,
        Right
    }
    public enum direction_Y
    {
        Stop,
        Up,
        Down
    }

    public class Engine_Helper
    {

        public static List<Bullet_Helper> list_Bullet = new List<Bullet_Helper>();//子弹集合
        public static List<EnemyPlane_Helper> list_EnemyPlane = new List<EnemyPlane_Helper>();//敌机集合


        public static WinMain_helper win_helper;

        public static direction_X Direction_X = direction_X.Stop;
        public static direction_Y Direction_Y = direction_Y.Stop;

        static int EnemyPlane_Point = 500;//敌机 X 位置( Y 默认从上向下)

        static double x = 300;//自己飞机初始位置
        static double y = 420;//自己飞机初始位置
        static double v = 5;//自己飞机飞行速度

        static Random random = new Random();
        public static bool IsOver = false;
        //-----主界面控件
        public static Window WinMain = null;
        public static Panel Win_canvas = null;

        public static UserControl BackImg;
        public static void AddBack()
        {
            BackImg = new UCBackGroundImg();
            VMParentCls vm = new BackGroundImg_Hleper(BackImg);
            Win_canvas.Children.Add(BackImg);
        }

        //增加自己飞机
        public static Control_MyPlane myPlane;
        public static void Add_MyPlane()
        {
            myPlane = new Control_MyPlane();
            Win_canvas.Children.Add(myPlane);
            Myplane_Helper myp = new Myplane_Helper(x, y, 3, false, myPlane, 40, 40);
            myp.Init();
        }

        //创建子弹
        static void Add_Bullet()
        {
            WinMain.Dispatcher.Invoke(new Action(() =>
            {
                Control_Bullet bui = new Control_Bullet();
                Bullet_Helper bu = new Bullet_Helper((myPlane.DataContext as Myplane_Helper).Left + 20, (myPlane.DataContext as Myplane_Helper).Top - 7, false, bui);
                Win_canvas.Children.Add(bui);
                list_Bullet.Add(bu);
            }));
        }
        //创建敌机
        static int EnemyPlaneCount = 0;
        static void Add_EnemyPlane()
        {
            EnemyPlaneCount++;
            WinMain.Dispatcher.Invoke(new Action(() =>
            {
                Control_EnemyPlane enemyPlane = new Control_EnemyPlane();
                int TmpPlane_V = random.Next(1, 6);
                EnemyPlane_Helper enemyHelper = new EnemyPlane_Helper(random.Next(1, EnemyPlane_Point), -80, TmpPlane_V,
                    new BitmapImage(new Uri(@"..\Imge\" + TmpPlane_V + ".png",
                    UriKind.Relative)), 3 - (TmpPlane_V * 0.25), enemyPlane, 30 + (TmpPlane_V * 6), 30 + (TmpPlane_V * 6), "Plane_" + EnemyPlaneCount);
                Win_canvas.Children.Add(enemyPlane);
                list_EnemyPlane.Add(enemyHelper);
            }));
        }

        //监听键盘并移动飞机(键盘上下左右控制自己飞机位置)
        static void OnKey_MouseMove()
        {
            if (Direction_X == direction_X.Left && (0 < x - v))
                x -= v;
            if (Direction_X == direction_X.Right && x - v < 560)
                x += v;
            if (Direction_Y == direction_Y.Up && 0 < y - v)
                y -= v;
            if (Direction_Y == direction_Y.Down && y - v < 430)
                y += v;

            WinMain.Dispatcher.BeginInvoke(new Action(() =>
            {
                (myPlane.DataContext as Myplane_Helper).Left = x;
                (myPlane.DataContext as Myplane_Helper).Top = y;
                //Myplane_Helper myp = new Myplane_Helper(x, y, 3, false, myPlane);
            }));
        }

        //敌机时间
        static int elapseBadPlane = 0;
        static int generBadPlane = 0;
        //子弹时间
        static int Bullet_num = 0;
        //-----------【主线程】----------------
        public static void Plane_Engine()
        {
            Thread thread = new Thread(() =>
              {
                  while (1 == 1)
                  {
                      //背景移动
                      WinMain.Dispatcher.Invoke(new Action(() =>
                          {
                              (BackImg.DataContext as VMParentCls).Run(1);
                          }));

                      //-------键盘监听并移动飞机
                      if (!IsOver)
                      {
                          OnKey_MouseMove();
                      }
                      else
                      {
                          WinMain.Dispatcher.Invoke(new Action(() =>
                          {
                              (myPlane.DataContext as Myplane_Helper).IsDead = true;
                          }));
                      }


                      #region //-------创建敌机
                      elapseBadPlane += 16;
                      if (elapseBadPlane >= generBadPlane)
                      {
                          generBadPlane = new Random().Next(1, 6) * 200;
                          elapseBadPlane = 0;
                          Add_EnemyPlane();
                      }
                      //优化界面（在界面去掉已经死掉的飞机和没用的子弹（出去隐藏控件））
                      WinMain.Dispatcher.Invoke(new Action(() =>
                      {
                          for (int i = Win_canvas.Children.Count - 1; i >= 0; i--)
                          {
                              if ((Win_canvas.Children[i] as FrameworkElement).Visibility != Visibility.Visible)
                              {
                                  Win_canvas.Children.Remove(Win_canvas.Children[i] as FrameworkElement);
                              }
                          }
                      }));

                      list_EnemyPlane.RemoveAll(s => s.isDead);
                      foreach (var item in list_EnemyPlane)
                      {
                          item.run();
                      }
                      #endregion

                      #region //-------创建子弹  和   判断敌机中弹
                      Bullet_num += 16;
                      if (Bullet_num >= 100)
                      {
                          Bullet_num = 0;
                          if (!IsOver)
                          {
                              Add_Bullet();
                          }
                      }
                      list_Bullet.RemoveAll(s => s.isDead);
                      foreach (var item in list_Bullet)
                      {
                          item.run();//循环敌机
                          #region //判断是否中弹
                          if (list_EnemyPlane.Count(s => item.Left > s.Left && item.Left + 2 < s.Left + s.Width_ && item.Top + 7 <= s.Top + s.Height_ && item.Top > s.Top) > 0)
                          {
                              foreach (var itemC in list_EnemyPlane.Where(s => item.Left > s.Left && item.Left + 2 < s.Left + s.Width_ && item.Top <= s.Top + s.Height_))
                              {
                                  itemC.Blood--;
                                  if (itemC.Blood <= 0)
                                  {
                                      itemC.isDead = true;
                                      itemC.Vis = System.Windows.Visibility.Collapsed;
                                  }
                                  WinMain.Dispatcher.Invoke(new Action(() =>
                                      {
                                          win_helper.Fraction += 10 + Convert.ToInt32(itemC.Tmp_blood * 5);
                                          (myPlane.DataContext as Myplane_Helper).GetProtectNeedValue += 10 + Convert.ToInt32(itemC.Tmp_blood * 5);
                                      }));

                                  WinMain.Dispatcher.Invoke(new Action(() =>
                                      {
                                          if ((myPlane.DataContext as Myplane_Helper).GetProtectNeedValue > 500 && !(myPlane.DataContext as Myplane_Helper).IsProtect)
                                          {
                                              (myPlane.DataContext as Myplane_Helper).IsProtect = true;
                                              (myPlane.DataContext as Myplane_Helper).protectValue = 3;
                                              win_helper.Protect = (myPlane.DataContext as Myplane_Helper).protectValue;
                                          }
                                      }));

                              }
                              item.isDead = true;
                              item.Vis = System.Windows.Visibility.Collapsed;

                          }
                          #endregion
                      }
                      #endregion

                      #region  //------循环敌机，判断是否与自己飞机撞击
                      if (!IsOver)
                      {
                          WinMain.Dispatcher.Invoke(new Action(() =>
                          {
                              Myplane_Helper mypl = myPlane.DataContext as Myplane_Helper;
                              if (list_EnemyPlane.Count(s => !s.isDead && Math.Abs(mypl.Left - s.Left) < (mypl.Left < s.Left ? mypl.Width_ : s.Width_) &&
                                  Math.Abs(mypl.Top - s.Top) < (mypl.Top < s.Top ? mypl.Height_ : s.Height_)) > 0)
                              {
                                  if (mypl.IsProtect && mypl.protectValue > 0)
                                  {
                                      mypl.protectValue--;
                                      win_helper.Protect = (myPlane.DataContext as Myplane_Helper).protectValue;
                                      foreach (var item in list_EnemyPlane.Where(s => !s.isDead && Math.Abs(mypl.Left - s.Left) < (mypl.Left < s.Left ? mypl.Width_ : s.Width_) &&
                                  Math.Abs(mypl.Top - s.Top) < (mypl.Top < s.Top ? mypl.Height_ : s.Height_)))
                                      {
                                          item.Vis = Visibility.Collapsed;
                                          item.isDead = true;
                                      }
                                      if (mypl.protectValue <= 0)
                                      {
                                          mypl.IsProtect = false;
                                          mypl.GetProtectNeedValue = 0;
                                      }
                                  }
                                  else
                                  {
                                      IsOver = true;
                                      MessageBox.Show("     Game   Over    ! !   ");
                                  }
                              }
                          }));
                      }
                      #endregion
                      Thread.Sleep(16);
                  }
              });
            thread.IsBackground = true;
            thread.Start();
        }


    }
}
